import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

/*
 * Block.java
 * This file contains a representation of a track block in the track model of the CTCSS
 * Author: Daniel R Hartman
 * Date Created: 2012-11-22
 * Date Last Updated: 2012-11-26
 */

/**
 * This class represents a track block
 * @author Daniel R Hartman
 */

public class Block extends JPanel implements ActionListener
{

        int number, type; //types are 0 = normal, 1 = station, 2 = switch, 3 = crossing//
        int speedLimit, fail;//fail states are 1 = broken rail, 2 = track circuit failure, 3 = power failure//
        int line, nextnum, prevnum, switchtonum;
        String switchToNumber;
        double grade, length;
        boolean occupied, closed, underground;
        Block prev, next, switchTo;
        //TrainModel train;
        ImageIcon icon;
        
        /**
         * Constructor for a switching track block
         * @param n block number
         * @param l block length
         * @param g block grade
         * @param sl block speed limit
         * @param u true means that a block is underground
         * @param t     block type
         * @param next the next block in the track
         * @param prev the previous block in the track
         * @param st the block that this block switches to.
         */
        public Block(int line, int n, double l, double g, int sl, boolean u, int t, int next, int prev, String st)
        {
        		this.line = line;
                number = n;
                type = t;
                speedLimit = sl;
                fail = 0;
                switchToNumber = st;
                grade = g;
                length = l;
                occupied = false;
                closed = false;
                underground = u;
                nextnum = next;
                prevnum = prev;
                switchtonum = Integer.parseInt(st);
        }
        


        /**
         * Constructor for a non-switching track block
         * @param n block number
         * @param l block length
         * @param g block grade
         * @param sl block speed limit
         * @param u true means that a block is underground
         * @param t     block type
         * @param next the next block in the track
         * @param prev the previous block in the track, null if block is in a one way section
         */
        public Block(int line, int n, double l, double g, int sl, boolean u, int t, int next, int prev)
        {
        		this.line = line;
                number = n;
                type = t;
                speedLimit = sl;
                fail = 0;
                grade = g;
                length = l;
                occupied = false;
                closed = false;
                underground = u;
                nextnum = next;
                prevnum = prev;
        }
        
        public void trainOnBlock(/*TrainModel t*/){
        	//train = t;
        	occupied = true;
        	//prev.train = null;
        	prev.occupied = false;
        	//next.train = null;
        	next.occupied = false;
        	if(line == 1)
        	{
        		TrackModelPane.red[prev.number].setIcon(prev.icon);
        		TrackModelPane.red[next.number].setIcon(next.icon);
        		TrackModelPane.red[number].setIcon(new ImageIcon("occupied.gif"));
        	}else
        	{
        		TrackModelPane.green[prev.number].setIcon(prev.icon);
        		TrackModelPane.green[next.number].setIcon(next.icon);
        		TrackModelPane.green[number].setIcon(new ImageIcon("occupied.gif"));
        	}
        }

        /**
         * @param failure failure mode
         */
        public void fail(int failure)
        {
                fail = failure;
        }

        /**
         * 
         */
        public void print()
        {

                if(switchTo != null)
                {
                        System.out.println(number + " " + type + " " + speedLimit + " " + length + " " + grade + " " + underground + " " + next.number + " " + switchToNumber);

                }else
                {
                        System.out.println(number + " " + type + " " + speedLimit + " " + length + " " + grade + " " + underground + " " + next.number);

                }
        }

        /**
         * updates the GUI when a block is selected
         * @param a action of selecting a block
         */
        @Override
        public void actionPerformed(ActionEvent a)
        {
                TrackModelPane.blockLabel.setText(TrackModelPane.blockNumber + number);
                TrackModelPane.speedLabel.setText(TrackModelPane.speed + speedLimit);
                TrackModelPane.lengthLabel.setText(TrackModelPane.length + length);
                TrackModelPane.gradeLabel.setText(TrackModelPane.grade + grade);
                switch(type)
                {
                case 0: TrackModelPane.typeLabel.setText(TrackModelPane.type + "Normal");
                break;
                case 1: TrackModelPane.typeLabel.setText(TrackModelPane.type + "Station");
                break;
                case 2: TrackModelPane.typeLabel.setText(TrackModelPane.type + "Switch");
                break;
                case 3: TrackModelPane.typeLabel.setText(TrackModelPane.type + "Railcrossing");
                break;
                }


                if(fail == 1)
                {
                        TrackModelPane.stateLabel.setText("FAILURE: Broken Rail");
                }else if(fail == 2)
                {
                        TrackModelPane.stateLabel.setText("FAILURE: Track Circuit Failure");
                }else if(fail == 3)
                {
                        TrackModelPane.stateLabel.setText("FAILURE: Power Failure");
                }else if(closed)
                {
                        TrackModelPane.stateLabel.setText("State: Closed");
                }else if(occupied)
                {
                        TrackModelPane.stateLabel.setText("State: Occupied");
                }else
                {
                        TrackModelPane.stateLabel.setText("State: Open");
                }


        }

}
